package com.itshare.video.controller;

import com.itshare.video.common.annontation.AttrData;
import com.itshare.video.common.annontation.Page;
import com.itshare.video.common.annontation.Size;
import com.itshare.video.common.aop.xss.annontation.CleanXSS;
import com.itshare.video.common.core.constant.enums.ApiInfo;
import com.itshare.video.common.core.constant.enums.DeleteStatus;
import com.itshare.video.common.dto.PageEntity;
import com.itshare.video.common.dto.PageParam;
import com.itshare.video.common.dto.QueryInfo;
import com.itshare.video.common.dto.Result;
import com.itshare.video.pojo.bo.AdvertisementDTO;
import com.itshare.video.pojo.bo.AdvertisementTimeChangeBO;
import com.itshare.video.pojo.bo.UpdateAdvertisementBO;
import com.itshare.video.pojo.code.PermitStatus;
import com.itshare.video.pojo.dto.UserTokenDTO;
import com.itshare.video.pojo.query.AdvertisementQuery;
import com.itshare.video.service.AdvertisementService;
import com.itshare.video.util.mapping.AdvertisementDTOMapping;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.mapstruct.factory.Mappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 广告业务控制
 *
 * @Author lyr
 * @create 2020/11/4 10:03
 */
@RestController
@RequestMapping("/api")
public class AdvertisementController {

    private AdvertisementDTOMapping advertisementDTOMapping = Mappers.getMapper(AdvertisementDTOMapping.class);

    @Resource
    private AdvertisementService advertisementService;
    //1. 用户查看自己发布的广告 ，
    //2. 用户请求发布自己的广告，  消息通知用户，等待审核
    //3. 管理员审核广告
    //4. 如果通过前端展示，消息提示用户
    //5. 如果不通过，消息提示用户

    /**
     * 用户发布广告
     *
     * @param advertisementDTO
     * @param userTokenDTO
     * @return
     */
    //7. 用户修改自己发布的广告，但是 是那种还没有进行审核的
    //8. 管理员查看所有发布，经过审核的广告，异常的，立刻下架
    @PostMapping("/advertisement")
    @CleanXSS

    public Result createAdvertisement(@RequestBody @Validated AdvertisementDTO advertisementDTO, @AttrData UserTokenDTO userTokenDTO) {
        advertisementService.addAdvertisement(advertisementDTO, userTokenDTO);
        return Result.of(ApiInfo.OK);
    }

    /**
     * 删除广告
     *
     * @param advertisementId
     * @return
     */
    @DeleteMapping("/advertisement")
    @RequiresPermissions("manager:video:all")
    public Result deleteAdvertisement(@RequestParam Integer advertisementId) {
        advertisementService.logicDeleteAdvertisement(advertisementId);
        return Result.success();
    }


    /**
     * 管理员获取待审核的
     *
     * @param page
     * @param size
     * @param userTokenDTO
     * @return
     */
    @RequiresPermissions("manager:video:all")
    @GetMapping("/advertisement_not_permited")
    public Result noPermitedInfo(@Page Integer page,
                                 @Size Integer size,
                                 @AttrData UserTokenDTO userTokenDTO) {
        //查询没有审核通过，并且没有被删除的
        AdvertisementQuery query = new AdvertisementQuery();
        query.setDeleteStatus(DeleteStatus.no_deleted.type)
                .setPermitStatus(PermitStatus.tobe_permitted.type);
        return Result.of(ApiInfo.OK)
                .setData(
                        PageEntity.of(
                                advertisementService.getAdvertisementAllField(userTokenDTO, QueryInfo.of(PageParam.of(page, size), query))
                        )
                );
    }

    @RequiresPermissions("manager:video:all")
    @GetMapping("/advertisement_permited")
    public Result<?> noPermitedInfo2(@Page Integer page,
                                     @Size Integer size,
                                     @AttrData UserTokenDTO userTokenDTO) {
        //查询没有审核通过，并且没有被删除的
        AdvertisementQuery query = new AdvertisementQuery();
        query.setDeleteStatus(DeleteStatus.no_deleted.type)
                .setPermitStatus(PermitStatus.permitted.type);
        return Result.of(ApiInfo.OK)
                .setData(
                        PageEntity.of(
                                advertisementService.getAdvertisementAllField(userTokenDTO, QueryInfo.of(PageParam.of(page, size), query))
                        )
                );
    }

    /**
     * 前端展示的
     *
     * @param page
     * @param size
     * @param userTokenDTO
     * @return
     */
    @GetMapping("/advertisement_front")
    public Result queryAllPermited(
            @RequestParam(required = false) Integer advertisementPutting,
            @Page Integer page,
            @Size Integer size,
            @AttrData UserTokenDTO userTokenDTO) {
        AdvertisementQuery query = new AdvertisementQuery()
                .setAdvertisementPutting(advertisementPutting)
                .setPermitStatus(PermitStatus.permitted.type)
                .setDeleteStatus(DeleteStatus.no_deleted.type);
        return Result.of(ApiInfo.OK)
                .setData(
                        PageEntity.of(
                                advertisementService.getAdvertisement(userTokenDTO, QueryInfo.of(PageParam.of(page, size), query))
                        )
                );
    }

    /**
     * 管理员选择下架 广告
     *
     * @param advertisementBO
     * @return
     */
    @RequiresPermissions("manager:video:all")
    @PutMapping("/advertisement")
    public Result setStatusOfAdvertisement(@RequestBody @Validated UpdateAdvertisementBO advertisementBO) {
        return advertisementService.updateAdvertisementStatus(advertisementBO);
    }


    /**
     * 获取我发布的广告
     *
     * @param userTokenDTO
     * @param page
     * @param size
     * @return
     */
    @GetMapping("my_advertisement")
    public Result myAdvertisement(@AttrData UserTokenDTO userTokenDTO, @Page Integer page, @Size Integer size) {
        return Result.of(ApiInfo.OK)
                .setData(
                        PageEntity.of(
                                advertisementService.getAdvertisement(userTokenDTO, QueryInfo.ofNullable(page, size,
                                        new AdvertisementQuery().setDeleteStatus(DeleteStatus.no_deleted.type)
                                                .setCreateBy(userTokenDTO.getId().toString())
                                ))
                        )
                );
    }
    //
    // @PutMapping("my_advertisement")
    // public Result
    //

    //管理员更改发布时间 beginTime 和 endTime
    @RequiresPermissions("manager:video:all")
    @PutMapping("/advertisement_property")
    public Result changeAdvertisementProperties(@RequestBody @Validated AdvertisementTimeChangeBO advertisementTimeChangeBO) {
        return advertisementService.updateRegionOrTime(advertisementTimeChangeBO);
    }


}
